﻿using System;
using System.Collections.Generic;
using System.Text;
using MicroLib;
using System.Data;
using MySql.Data.MySqlClient;
using MySql.Data;
using System.IO;
using System.Xml;

namespace JobAgent
{
    class Program
    {
        static string GetIdleJob()
        {
            string jobid="-1";

            string connStr = "server=" + Util.server + ";user=" + Util.sqluser + ";database=" + Util.database + ";port=" + Util.port + ";password=" + Util.sqlpasswd + ";charset=utf8;";
            MySqlConnection conn = new MySqlConnection(connStr);
            conn.Open();
            
            string sql = "call spGetFreeJob()";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            
            jobid = cmd.ExecuteScalar().ToString();
            
            conn.Close();

            return jobid;
        }
        static void BatchPost(string spname, string hostid, string degree,string message)
        {
            MySqlDataReader dr;
            int offset=500;
            int count = 0;
            int tmp = 0;

            string connStr = "server=" + Util.server + ";user=" + Util.sqluser + ";database=" + Util.database + ";port=" + Util.port + ";password=" + Util.sqlpasswd + ";charset=utf8;";
            MySqlConnection conn = new MySqlConnection(connStr);
            conn.Open();
            MySqlCommand cmd = new MySqlCommand();
            cmd.Connection=conn;
            string sql = "call " + spname + "(" + hostid + "," + degree + ",0,0,2,0,500)";
            cmd.CommandText = sql;
            dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                count = int.Parse(dr[0].ToString());
            }
            dr.Close();

            for (int i = 0; i < count; i += offset)
            {
                sql = "call " + spname + "(" + hostid + "," + degree + ",0,0,0," + i.ToString() + "," + offset.ToString() + ")";
                cmd.CommandText = sql;
                dr = cmd.ExecuteReader();
               
                while (dr.Read())
                {
                    try
                    {
                        sina.At2People((string)dr["name"], message);
                    }
                    catch (Exception e)
                    {
                        Util.AddLog(e.ToString() + "\nTry another account for " + dr["name"].ToString());
                        sina.Logout();
                        Util.GetAvailableAccount();
                        sina.Login(Util.user, Util.password, "");
                    }
                }
                         

                dr.Close();
            }
            conn.Close();
        }
       
        static void ProcessJob(string jobid)
        {
            string spname=null;
            string hostid=null;
            string message = null;
            int degree = 0;

            string connStr = "server=" + Util.server + ";user=" + Util.sqluser + ";database=" + Util.database + ";port=" + Util.port + ";password=" + Util.sqlpasswd + ";charset=utf8;";
            MySqlConnection conn = new MySqlConnection(connStr);
            conn.Open();

            string sql = "select * from queryjobs where jobid="+jobid;
            MySqlCommand cmd = new MySqlCommand(sql, conn);

            MySqlDataReader  dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                spname = (string)dr["spname"];
                degree = (int)dr["degree"];
                hostid = (string)dr["hostid"];
                message = (string)dr["message"];
            }
            
            conn.Close();

            if (spname != null&&degree>0)
            {
                for (int i = 1; i <=degree; i++)
                {
                    BatchPost(spname, hostid, degree.ToString(),message);
                }

            }
            

        }
        static MicroLib.MicroBlogSina sina = new MicroLib.MicroBlogSina();
        static void Main(string[] args)
        {
            string jobid;
            Util.InitConfig();
            Util.GetAvailableAccount();
            sina.Login(Util.user, Util.password, "");

            while (true)
            {
                jobid = GetIdleJob();
                ProcessJob(jobid);
            }
        }
    }
}
